package cn.sccl.dirms.security.controller;

import cn.sccl.dirms.security.model.RoleExtend;
import cn.sccl.dirms.security.model.Role;
import cn.sccl.dirms.security.model.UserAndRole;
import cn.sccl.dirms.security.service.UserAndRoleManager;
import com.fasterxml.jackson.annotation.JsonView;
import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@RestController
@RequestMapping(value = "/dirms/security/userAndRole")
public class UserAndRoleController extends BaseController {

    @Autowired
    private UserAndRoleManager userAndRoleManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<UserAndRole> listAllUserAndRole() {
        return userAndRoleManager.listAll();
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<UserAndRole> pageQueryUserAndRole(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return userAndRoleManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param userAndRole
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public UserAndRole saveUserAndRole(@RequestBody UserAndRole userAndRole) {
        userAndRoleManager.save(userAndRole);
        return userAndRole;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST,RequestMethod.GET})
    @JsonView(Object.class)
    public boolean deleteUserAndRole(@PathVariable("id") String id) {
        userAndRoleManager.delete(id);
        return true ;
    }


    /**
     * 用户可用权限分页查询
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/availPage", method = RequestMethod.GET)
    public PageResult<Role> pageQueryUserAvailRole(HttpServletRequest request) {
        return userAndRoleManager.getUserAvailRoleInfo(getPageIndex(request), getPageSize(request), getQueryData(request));
       /* return roleManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));*/
    }

    /**
     * 用户拥有权限分页查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/existPage" , method = RequestMethod.GET )
    public PageResult<RoleExtend> pageQueryUserRole(HttpServletRequest request){
        return userAndRoleManager.getUserRoleInfo(getPageIndex(request),getPageSize(request),getQueryData(request));
       /* return roleManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));*/
    }
}
